<?php

namespace Home\Controller\SaleManager;

use Home\Controller\SaleManager\BasicController;
use Home\Model\SaleManager\City\SaleManagerModel;
use Home\Model\SaleManager\Clinic\ClinicModel;
use Home\Common\WorksummerHelper;
use Think\Model;
class SalemanagerController extends BasicController{
	protected  $clinicMod; 
	protected $saleMod;
	function _initialize() {
		$this->clinicMod = new ClinicModel();
		$this->saleMod = new SaleManagerModel();
		parent::_initialize($this->saleMod);
	}
	
	public function editAssistant(){
		$REQUEST = ( array ) I ( 'request.' );
		if(IS_POST){
			if (IS_AJAX) {
				$model = new Model();
				$model->startTrans();
				$data ['id'] = intval(think_ucenter_decrypt($REQUEST ['userId']));
				$data ['phone'] = intval($REQUEST ['phone']);
				$checkObj =$this->saleMod->findInfobyCondition(array('phone'=>$data ['phone']));
				if(!empty($checkObj) && (intval($checkObj[0]['id'])!=intval($data ['id']))){
					responseJson(200,'该手机号已存在','该手机号已存在');
				}
				$data ['card_number'] = $REQUEST ['card_number'];
				$data ['sex'] = $REQUEST ['sex'];
				$data ['age'] = $REQUEST ['age'];
				$data ['manage_locations'] = $REQUEST ['belong_locations'];
				$result= $this->saleMod->updateById($data ['id'], $data);
				//省总
				$clinics=json_decode(html_entity_decode($REQUEST['province']),true);
				$map['id']=array('IN',implode(',',$clinics));
				$re = D('saleman')->where($map)->setField(array('superior_id'=>$data ['id']));
				if($re==false){
					$model->rollback();
					responseJson($result['status'],$result['msg'],$result['data']);
				}
				$model->commit();
				responseJson($result['status'],$result['msg'],$result['data']);
			}
		}else{
			if(!empty($REQUEST['userId'])){
				$userId = think_ucenter_decrypt($REQUEST['userId']);
				//获取该大区经理下面的所有省总
				$childrens = WorksummerHelper::getAllChildrenId(self::$user['id']);
				$map['id']  = array('in',$childrens);
				$map['role_id']=2;
				$provinces = D('saleman')->where($map)->select();
				$targetAssistant = D('saleman')->where(array('id'=>$userId))->find();
				$this->assign('targetAssistant',$targetAssistant);
				$this->assign('userId',$userId);
				$this->assign('provinces',$provinces);
			}else{
				$this->assign('msg','参数错误！');
			}
			$this->display('editassistant');
		}
	}
}
